class CharactersController < ApplicationController
  before_filter :require_officer, :except => [ :index, :show ]

  def create
    @character = Character.new(params[:character])
    if @character.save
      flash[:notice] = "#{@character.name} created."
      redirect_to :back
    end
  end

  def edit
    @character = Character.find(params[:id])
    @page_description = @character.name
    @users = User.find(:all)
    @roles = Role.find(:all)
    @roles_not_filled = @roles.collect { |r|
                          [r.name, r.id] unless @character.roles.include?(r)
                        }.compact
  end

  def update
    character = Character.find(params[:id])
    active_before_update = character.active?
    if character.update_attributes(params[:character])
      flash[:notice] = "#{character.name} updated."
    else
      flash[:notice] = "Failed to update #{character.name}."
    end

    if active_before_update != character.active?
      expire_fragment("characters/dkp_table")
      Reward.update_total_number_of_shares
    end
    redirect_to :action => :edit
  end

  def index
    @page_description = "Characters"
    @dkp_columns = []
    all_characters = Character.find(:all, :order => "active, character_class_id, name")
    active_characters = (all_characters.collect { |c| c if c.active? }).compact
    column_count = 1
    column = []
    (active_characters.sort { |a, b| b.dkp <=> a.dkp }).each { |c|
      if (column_count % 16 == 0)
        @dkp_columns << column
        column = []
        column_count = 1
      end
      column << c
      column_count += 1
    }

    @dkp_columns << column

    @classes = CharacterClass.find(:all)
    if params[:format] == "lua"
      rank = 1
      lua_data = "NurfedDKPPlayers = {\n"
      (@all_characters.sort { |a,b| b.dkp <=> a.dkp }).each { |char|
        next unless char.active?
        lua_data += "\t[\"#{char.name}\"] = {\n"
        lua_data += "\t\t[\"rank\"] = #{rank},\n"
        lua_data += sprintf("\t\t[\"dkp\"] = %.3f,\n", char.dkp)
        lua_data += "\t\t[\"class\"] = \"#{char.character_class.name}\",\n"
        lua_data += "\t\t[\"att\"] = 100.0,\n"
        lua_data += "\t},\n"
        rank = rank + 1
      }
      lua_data += "}\n"
      render :text => lua_data
    end
  end

  def show
    if is_officer?
      redirect_to :action => :edit
    else
      @character = Character.find(params[:id])
      @page_description = @character.name
    end
  end

  def add_role
    character = Character.find(params[:id])
    character.roles << Role.find(params[:characters_role][:role_id])
    character.save
    redirect_to :back
  end

  def delete_roles
    character = Character.find(params[:id])
    roles_were_deleted = false
    params.each_key { |k|
      if (k =~ /^deleterole([[:digit:]]+)$/)
        CharactersRole.find(:first, :conditions => [ "character_id = ? AND role_id = ?", character.id, $1.to_i ]).destroy
        roles_were_deleted = true
      end
    }
    if roles_were_deleted
      flash[:notice] = "Roles deleted."
    else
      flash[:notice] = "Select roles to delete."
    end
    redirect_to :action => :edit, :id => character.id
  end

end
